<template>
  <div class="head-container">
    <!-- 搜索 -->
    <div v-show="sup_this.searchToggle">
      <el-select v-model="paramBean.processStatus" size="mini" style="width: 140px;" clearable placeholder="请选择收款状态">
        <el-option v-for="item in dicts.fin_process_status" :key="item.id" :label="item.dictName" :value="item.dictCode"/>
      </el-select>
      <el-select v-model="paramBean.invoiceStatus" size="mini" style="width: 140px;" clearable placeholder="请选择开票状态">
        <el-option v-for="item in dicts.fin_invoice_status" :key="item.id" :label="item.dictName" :value="item.dictCode"/>
      </el-select>
      <el-input v-model="paramBean.busOtherCpyName" clearable placeholder="输入客户搜索" style="width: 200px;" class="filter-item" @keyup.enter.native="sup_this.toQuery"/>
      <el-input v-model="paramBean.sourceCode" clearable placeholder="输入订单号搜索" style="width: 200px;" class="filter-item" @keyup.enter.native="sup_this.toQuery"/>
      <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="sup_this.toQuery">搜索</el-button>
      <el-button class="filter-item" size="mini" type="warning" icon="el-icon-refresh-left" @click="sup_this.resetQuery()">重置</el-button>
    </div>
    <div class="crud-opts">
      <el-button
        v-permission="['SYS_ADMIN','COMMON_ADMIN','FINTRX_RCV_ALL','FINTRX_RCV_CREATE','FINTRX_RCV_CHANGE']"
        class="filter-item"
        size="mini"
        type="primary"
        icon="el-icon-plus"
        @click="toReceive">收款登记</el-button>
      <el-button
        v-loading="buttonLoading"
        v-permission="['SYS_ADMIN','COMMON_ADMIN','FINTRX_RCV_ALL','FINTRX_RCV_CREATE','FINTRX_RCV_CHANGE']"
        class="filter-item"
        size="mini"
        type="warning"
        icon="el-icon-finished"
        @click="doManualProcessRcv">标识已收款</el-button>
      <el-button
        v-permission="['SYS_ADMIN','COMMON_ADMIN','FIN_INVOICE_ALL','FIN_INVOICE_CREATE','FIN_INVOICE_CHANGE']"
        class="filter-item"
        size="mini"
        type="primary"
        icon="el-icon-plus"
        @click="toInvoice">开票</el-button>
      <DataGridOpts :sup_this="sup_this" @refreshClick="refreshClick" @searchClick="searchClick" />
    </div>
    <invoice-form ref="invoiceForm" :sup_this="sup_this" />
    <rcv-form ref="rcvForm" :sup_this="sup_this" />
  </div>
</template>

<script>
import finWait from '@/api/fin/FinWait'
import RcvForm from '@/views/fin/fintrx/module/rcvForm'
import invoiceForm from '@/views/fin/invoice/module/form'

export default {
  components: { RcvForm, invoiceForm },
  dictTypes: ['fin_process_status', 'fin_invoice_status'],
  props: {
    paramBean: {
      type: Object,
      required: true
    },
    sup_this: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      deleteLoading: false,
      submitLoading: false,
      destoryLoading: false,
      buttonLoading: false
    }
  },
  methods: {
    refreshClick() {
      this.sup_this.init()
    },
    searchClick() {
      this.sup_this.toggleSearch()
    },
    beforeCreate() {
    },
    doMultiDelete() {
      const _this = this.sup_this
      _this.doMultiDelete(finWait.deleteUrl(), this)
    },
    toCopy() {
      const _this = this.sup_this
      const selectMainId = _this.getSelectMainId()
      if (!selectMainId) {
        return
      }
      const _thisForm = _this.$refs.form
      _thisForm.initDataOfBefore()
      _thisForm.copyData(selectMainId)
    },
    toAdd() {
      const _this = this.sup_this.$refs.form
      _this.initDataOfBefore()
      _this.addData()
    },
    toReceive() {
      const _this = this.sup_this
      const busRelList = []
      let repeatKey = null
      let diffFlag = false
      _this.selectedGridRows.forEach(row => {
        const tmpkey = row.otherSysOwnerCpy + row.priceCur + row.oriPriceCur + row.invoiceType
        if (repeatKey && repeatKey !== tmpkey) {
          diffFlag = true
          return
        }
        repeatKey = tmpkey
        busRelList.push({ fromBusModule: row.busModule, fromBusClass: row.busClass, fromCode: row.sysGenCode, fromId: row.id })
      })
      if (diffFlag) {
        this.$message({
          message: '请选择相同客户+币种+发票类型',
          type: 'warning'
        })
        return
      }
      if (busRelList.length === 0) {
        this.$message({
          message: '请选择记录',
          type: 'warning'
        })
        return
      }
      const _thisform = this.$refs.rcvForm
      _thisform.initDataOfBefore()
      _thisform.addDataFromWaitOrder(busRelList)
    },
    toInvoice() {
      const _this = this.sup_this
      const busRelList = []
      let repeatKey = null
      let diffFlag = false
      _this.selectedGridRows.forEach(row => {
        const tmpkey = row.otherSysOwnerCpy + row.priceCur + row.invoiceType
        if (repeatKey && repeatKey !== tmpkey) {
          diffFlag = true
          return
        }
        repeatKey = tmpkey
        busRelList.push({ fromBusModule: row.busModule, fromBusClass: row.busClass, fromCode: row.sysGenCode, fromId: row.id })
      })
      if (diffFlag) {
        this.$message({
          message: '请选择相同客户+币种+发票类型',
          type: 'warning'
        })
        return
      }
      if (busRelList.length === 0) {
        this.$message({
          message: '请选择记录',
          type: 'warning'
        })
        return
      }
      const _thisform = this.$refs.invoiceForm
      _thisform.initDataOfBefore()
      _thisform.addDataFromWaitOrder(busRelList)
    },
    doManualProcessRcv() {
      const _this = this.sup_this
      const selectIds = []
      _this.selectedGridRows.forEach(row => {
        selectIds.push(row.id)
      })
      if (selectIds.length === 0) {
        this.$message({
          message: '请选择记录',
          type: 'warning'
        })
        return
      }
      this.buttonLoading = true
      this.$confirm('确认要手工标记已处理?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        const data = { idList: selectIds }
        finWait.doManualProcessRcv(data).then(res => {
          this.buttonLoading = false
          _this.init()
        }).catch(err => {
          this.buttonLoading = false
          console.log(err)
        })
      }).catch(() => {
        this.buttonLoading = false
      })
    }
  }
}
</script>
